<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        class Pet {
          #nickname;
          #age;
          constructor(nickname, age) {
            this.#nickname = nickname;
            this.setAge(age);
          }
          get nickname() { return this.#nickname; }
          set nickname(n) { this.#nickname = n; }
          get age() { return this.#age; }
          set age(a) {
            if (a >= 0 && a <= 30) this.#age = a;
            else throw new Error('年龄不合法');
          }
        }
        class Dog extends Pet {
          #color;
          constructor(nickname, age, color) {
            super(nickname, age);
            this.#color = color;
          }
          get color() { return this.#color; }
          set color(c) { this.#color = c; }
        }
        try {
          let dog = new Dog('旺财', 3, '黄色');
          console.log("宠物昵称：", dog.nickname);
          console.log("宠物年龄：", dog.age);
          console.log("宠物颜色：", dog.color);
          dog.age = 40;
        } catch (e) {
          console.log(e.message);
        }
      </script>
</body>
</html>